package com.xytc.company.company_backend.adminEndPoint.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xytc.company.company_backend.adminEndPoint.entity.dao.CompanyInfoDao;
import com.xytc.company.company_backend.adminEndPoint.entity.rto.CompanyInfoRto;
import com.xytc.company.company_backend.adminEndPoint.mapper.ICompanyInfoMapper;
import com.xytc.company.company_backend.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/company/companyInfoManage")
public class CompanyInfoManageController {

    @Autowired
    private ICompanyInfoMapper companyInfoMapper;

    /**
     * 接口2.1 新增或编辑菜品信息
     *
     * @param companyInfoRto
     * @return
     */
    @PostMapping("/addOrUpdateCompany")
    public Result<?> addOrUpdateCompany(@RequestBody CompanyInfoRto companyInfoRto) {

        CompanyInfoDao companyInfoDao = new CompanyInfoDao();

        companyInfoDao.setCompanyName(companyInfoRto.getCompanyName());
        companyInfoDao.setCompanyHr(companyInfoRto.getCompanyHr());
        companyInfoDao.setHrPhone(companyInfoRto.getHrPhone());
        companyInfoDao.setCompanyAddress(companyInfoRto.getCompanyAddress());
        companyInfoDao.setStatus(1); // 新增公司信息时，公司状态为默认值 1；编辑接口不涉及公司菜单 启用/禁用 状态的更改，启用/禁用由接口 xxx 来实现
        companyInfoDao.setDelFlag(0); // del_flag 标记为0；删除公司由接口 xxx 来实现
        if (StringUtils.isBlank(companyInfoDao.getId())) {
            // 新增菜品，先判断表中是否已经有同名的菜品，不允许添加重复店铺名称的店铺
            List<CompanyInfoDao> list = companyInfoMapper.selectList(
                    new LambdaQueryWrapper<CompanyInfoDao>()
                            .eq(CompanyInfoDao::getCompanyName, companyInfoRto.getCompanyName())
                            .eq(CompanyInfoDao::getDelFlag, 0));
            if (list != null && !list.isEmpty()) {
                return Result.error("公司【" + companyInfoRto.getCompanyName() + "】已存在，不能录入重复名称的公司");
            }
            companyInfoDao.setCreateTime(new Date()); // 新增公司时，创建时间为接口调用时间
            // 公司 id为null或空字符串，新增 公司
            companyInfoMapper.insert(companyInfoDao);
        } else {
            // 更新菜品信息
            companyInfoDao.setId(companyInfoRto.getId());
            // 菜品id不为null，也不为空字符串，更新菜品
            companyInfoMapper.updateById(companyInfoDao);
        }

        return Result.ok();
    }
}
